package com.john.permission.web;

import com.john.permission.common.util.GlobalSystemConstants;
import com.john.permission.pojo.AjaxResponsePOJO;
import com.john.permission.pojo.Permission;
import com.john.permission.pojo.Role;
import com.john.permission.pojo.User;
import com.john.permission.service.PermissionService;
import com.john.permission.service.RoleService;
import com.john.permission.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/login")
    public AjaxResponsePOJO login(User user, HttpSession session) {
        if(user == null) {
            return new AjaxResponsePOJO.Builder()
                    .code(GlobalSystemConstants.HTTP_CODE_FORBIDEN)
                    .msg("用户名或密码参数异常")
                    .build();
        }

        User userInDB = userService.checkUser(user);
        if(userInDB != null) {

            // 将用户信息保存到session中
            session.setAttribute("user", userInDB);

            // 返回登录成功
            return new AjaxResponsePOJO.Builder().ok("登录成功", "/main");
        }else {
            return new AjaxResponsePOJO.Builder()
                    .code(GlobalSystemConstants.HTTP_CODE_FORBIDEN)
                    .msg("用户名或密码错误，请重新输入！")
                    .build();
        }
    }

}
